using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;
using Cocbook_DAL;

namespace Cocbook___modul_sale
{
    public partial class frmSetupServer : Form
    {
        public frmSetupServer()
        {
            InitializeComponent();
        }
        Connection con = new Connection();



        public string CreateConnectString(int type)
        {
            string s = null;
            switch (type)
            {
                case 1:
                    s = "Data Source = "+txtIpAddress.Text+";" +
                                "Network Library = DBMSSOCN;" +
                                "Initial Catalog = Cocbook_data;" +
                                " User ID = "+txtID.Text+";" +
                                "Password = "+txtPassword.Text+";";
                    break;
                case 2:
                    s = "Server = "+txtIpAddress.Text+";"+
                                "Database = Cocbook_data;"+
                                " User ID = "+txtID.Text +
                                "; Password = "+txtPassword.Text;
                    break;
            }
            
            
            return s;
        }

        #region Giao dien

        private void rbtWinAut_CheckedChanged(object sender, EventArgs e)
        {
            txtID.Enabled = !rbtWinAut.Checked;
            txtPassword.Enabled = !rbtWinAut.Checked;
            if (rbtWinAut.Checked)
            {
                txtIpAddress.Text = System.Environment.MachineName+@"\SQLEXPRESS";
            }
            else
            {
                txtIpAddress.Text = "";
            }
        }
        private void txtIpAddress_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtIpAddress, DevComponents.DotNetBar.Validator.eHighlightColor.None);
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            highlighter1.SetHighlightColor(txtID, DevComponents.DotNetBar.Validator.eHighlightColor.None);
        }

        private void cbxUseTempData_CheckedChanged(object sender, EventArgs e)
        {
            txtIpAddress.Enabled = !cbxUseTempData.Checked;
            txtID.Enabled = !cbxUseTempData.Checked;
            txtPassword.Enabled = !cbxUseTempData.Checked;
            rbtSQLAut.Enabled = !cbxUseTempData.Checked;
            rbtWinAut.Enabled = !cbxUseTempData.Checked;
        }
        #endregion

        public void CheckConnect(string connectstring)
        {
            SqlConnection connection = new SqlConnection(connectstring);
            cpsStatus.Visible = true;
            connection.Open();
            connection.Close();
            cpsStatus.Visible = false;
            MessageBox.Show("Connected!");            
            con.SetConnectString(connectstring);
        }


        private void btnTest_Click(object sender, EventArgs e)
        {
            if (rbtSQLAut.Checked)
            {
                if (txtIpAddress.Text == "" || txtID.Text == "")
                {
                    if (txtIpAddress.Text == "")
                    {
                        highlighter1.SetHighlightColor(txtIpAddress, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    }
                    if (txtID.Text == "")
                    {
                        highlighter1.SetHighlightColor(txtID, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                    }
                }
                else
                {
                    try
                    {
                        CheckConnect(CreateConnectString(1));
                    }
                    catch (Exception ex)
                    {
                        cpsStatus.Visible = false;
                        DialogResult mess = MessageBox.Show(ex.ToString(), "Connect Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                        while (mess == DialogResult.Retry)
                        {
                            try
                            {
                                CheckConnect(CreateConnectString(1));
                                mess = System.Windows.Forms.DialogResult.Cancel;
                            }
                            catch (Exception exc)
                            {
                                cpsStatus.Visible = false;
                                mess = MessageBox.Show(exc.ToString(), "Connect Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                            }
                        }
                    }
                }
            }
            else
            {
                try
                {
                    CheckConnect(CreateConnectString(2));
                }
                catch (Exception ex)
                {
                    DialogResult mess = MessageBox.Show(ex.ToString(), "Connect Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) ;
                    while (mess == DialogResult.Retry)
                    {
                        try
                        {
                            CheckConnect(CreateConnectString(2));
                            mess = System.Windows.Forms.DialogResult.Cancel;
                        }
                        catch (Exception exc)
                        {
                            mess = MessageBox.Show(exc.ToString(), "Connect Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                        }
                    }
                    cpsStatus.Visible = false;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cbxUseTempData.Checked)
            {
                frmMain.status = false;
            }
            else
            {
                frmMain.status = true;
            }
            this.Close();
        }

        private void cpsStatus_VisibleChanged(object sender, EventArgs e)
        {
            cpsStatus.IsRunning = cpsStatus.Visible;
        }

        private void frmSetupServer_Load(object sender, EventArgs e)
        {

        }


    }








}